//
//  LCWComposeTextView.swift
//  MyWB
//
//  Created by 李成万 on 16/7/3.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

class LCWComposeTextView: UITextView {

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }

    var placeHolder : String?{
        didSet{
            placeHolderLabel.text = placeHolder
        }
    }
    
    override var font : UIFont?{
        didSet{
            placeHolderLabel.font = font
        }
    }
//    override var textColor : UIColor?{
//        didSet{
//            placeHolderLabel.textColor = textColor
//        }
//    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        addSubview(placeHolderLabel)
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(SCREENW - 10)
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
        
//        NSNotificationCenter.defaultCenter().addObserver(self, selector: "emotionShow:", name: EmotionShow, object: nil)
        
    }
    /**
     隐藏占位文字的通知方法
     */
    func textViewTextDidChange(){
        placeHolderLabel.hidden = self.hasText()
    }
    
//    @objc private func emotionShow(noti:NSNotification){
//        
//        guard let emotion = noti.object as? LCWEmotions else{
//            return
//        }
//        
//        if emotion.isEmoji {
////            let selectRange = self.selectedRange
////            self.text.insert(((emotion.code ?? "") as NSString).emoji(), atIndex: selectRange.location)
//            
//        }
//    }
    
    /// 占位字符
    private lazy var placeHolderLabel : UILabel = {
        let lab = UILabel(messageColor: UIColor.grayColor(), textFont: 14)
        
        lab.numberOfLines = 0
        return lab
    }()

    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
